package com.eleMall.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eleMall.entity.Category;
import com.eleMall.entity.Goods;
import com.eleMall.entity.Result;
import com.eleMall.service.CategoryService;
import com.eleMall.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ximen
 * @since 2024-03-10
 */
@RestController
@RequestMapping("/eleMall/goods")
@Api(tags = "商品(Goods)表控制层")
public class GoodsController {


    /**
     * 服务对象
     */
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("获取热门商品")
    @GetMapping("/getHotGoods")
    public Result<List<Goods>>getHotGoods(){
        return Result.success(goodsService.getHotGoods()) ;
    }

    /**
     * 分页查询
     *
     * @param pageSize
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<Page<Goods>> page(int page, int pageSize) {
        //分页构造器
        Page<Goods> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据id进行排序
        queryWrapper.orderByAsc(Goods::getId);

        //分页查询
        goodsService.page(pageInfo,queryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 分类查询
     *
     * @param sortName 分类名称
     * @return 查询结果
     */
    @ApiOperation("分类查询")
    @GetMapping("/getBySort")
    public Result<List<Goods>> getBySort(String sortName) {

        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Goods> queryWrapper2 = new LambdaQueryWrapper<>();
        Category category = categoryService.getOne(queryWrapper.eq(Category::getName, sortName));
        Long categoryId = category.getId();
        queryWrapper2 = queryWrapper2.eq(Goods::getSortId, categoryId);
        List<Goods> list = goodsService.list(queryWrapper2);

        return Result.success(list);
    }

    @ApiOperation("分类查询分页")
    @GetMapping("/getBySortPlus")
    public Result<Page<Goods>> getBySort(int current,int size,int sortName) {
        return Result.success(goodsService.getShowListPlus(current,size,sortName));
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("{id}")
    public Result queryById(@ApiParam(value = "id 主键") @PathVariable("id") Long id) {
        //根据id获得实体
        Goods goods = goodsService.getById(id);
        if(goods != null){
            goods.setClick(goods.getClick()+1);
            goodsService.updateById(goods);
        }
        return Result.success(goods);
    }

    /**
     * 新增数据
     *
     * @param goods 实体
     * @return 新增结果
     */
    @ApiOperation("新增数据")
    @PostMapping
    public Result add(@ApiParam(value = "goods 实体") @RequestBody Goods goods) {
        goodsService.save(goods);
        return Result.success("新增商品成功");
    }

    /**
     * 编辑数据
     *
     * @param goods 实体
     * @return 编辑结果
     */
    @ApiOperation("编辑数据")
    @PutMapping
    public Result edit(@ApiParam(value = "goods 实体") @RequestBody Goods goods) {
        goodsService.updateById(goods);
        return Result.success("修改商品数据成功");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除数据")
    @DeleteMapping
    public Result deleteById(@ApiParam(value = "id 主键") Long id) {
        if(id == null){
            return Result.error("id不能为空");
        }
        goodsService.removeById(id);
        return Result.success("删除成功");
    }

}
